<template>
  <div class="news-detail">
    <div
      class="page-header"
      :style="{ backgroundImage: `url(${bannerImage})` }"
    >
      <div class="container">
        <h1 class="page-title">新闻详情</h1>
        <p class="page-subtitle">查看完整的新闻内容</p>
      </div>
    </div>

    <div class="container">
      <div class="content-section">
        <!-- 加载状态 -->
        <div v-if="loading" class="loading-container">
          <el-skeleton :rows="5" animated />
        </div>

        <!-- 新闻内容 -->
        <div v-else-if="newsData" class="news-content">
          <!-- 新闻标题 -->
          <h1 class="news-title">{{ newsData.title }}</h1>

          <!-- 新闻元信息 -->
          <div class="news-meta">
            <span class="news-date">
              <el-icon><Calendar /></el-icon>
              {{ newsData.date }}
            </span>
            <span class="news-category">
              <el-icon><Collection /></el-icon>
              {{ newsData.category }}
            </span>
          </div>

          <!-- 新闻图片 -->
          <div v-if="newsData.image" class="news-image-section">
            <img
              :src="newsData.image"
              :alt="newsData.title"
              class="news-image"
            />
          </div>

          <!-- 新闻内容 -->
          <div class="news-body">
            <div v-html="newsData.content" class="news-full-content"></div>
          </div>

          <!-- 底部返回按钮 -->
          <div class="news-actions">
            <el-button @click="goBack" type="primary" size="large">
              <el-icon><ArrowLeft /></el-icon>
              返回列表
            </el-button>
          </div>
        </div>

        <!-- 无数据状态 -->
        <div v-else class="empty-container">
          <el-empty description="新闻不存在或已被删除">
            <el-button @click="goBack" type="primary">返回列表</el-button>
          </el-empty>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { subpageApi } from "@/api/user";
import { ElMessage } from "element-plus";
import { ArrowLeft, Calendar, Collection } from "@element-plus/icons-vue";
import { decrypt } from "@/utils/cryptoJS";

export default {
  name: "NewsDetail",
  components: {
    ArrowLeft,
    Calendar,
    Collection,
  },
  computed: {
    bannerImage() {
      return decrypt(this.$route.query.banner || "") || "";
    },
  },
  setup() {
    const route = useRoute();
    const router = useRouter();

    // 状态管理
    const loading = ref(true);
    const newsData = ref(null);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content)
        return { content: "", excerpt: "", image: "", date: "", category: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return {
          content: content,
          excerpt: "",
          image: "",
          date: "",
          category: "",
        };
      } catch (e) {
        return {
          content: content,
          excerpt: "",
          image: "",
          date: "",
          category: "",
        };
      }
    };

    // 加载新闻详情
    const loadNewsDetail = async () => {
      try {
        loading.value = true;
        const newsId = route.query.id;

        if (!newsId) {
          ElMessage.error("新闻ID不存在");
          goBack();
          return;
        }

        // 从展会资讯数据中查找对应的新闻
        const response = await subpageApi.getSubpageList(
          "media",
          "exhibition-news"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 查找指定ID的新闻
          const news = response.data.find((item) => item.id == newsId);

          if (news) {
            const parsed = parseContent(news.content);
            newsData.value = {
              id: news.id,
              title: news.title || "新闻标题",
              content:
                parsed.content ||
                news.content ||
                news.description ||
                "暂无内容",
              image: news.imageUrl || news.image || parsed.image || "",
              date: parsed.date || new Date().toLocaleDateString(),
              category: parsed.category || "展会资讯",
            };
          } else {
            ElMessage.error("新闻不存在");
            newsData.value = null;
          }
        } else {
          // 使用默认数据
          newsData.value = getDefaultNewsDetail(route.query.id);
        }
      } catch (error) {
        console.error("加载新闻详情失败:", error);
        ElMessage.error("加载新闻详情失败");
        newsData.value = null;
      } finally {
        loading.value = false;
      }
    };

    // 获取默认新闻详情
    const getDefaultNewsDetail = (newsId) => {
      const defaultNews = [
        {
          id: 1,
          title: "FAIR plus 2025展会筹备工作全面启动",
          content: `
            <h3>展会筹备工作全面启动</h3>
            <p>2025年FAIR plus机器人全产业链接会筹备工作已全面启动，预计将有超过500家展商参展。</p>
            <p>展会将在上海国家会展中心举办，展示最新的机器人技术和应用。本次展会将以"智能未来，共创辉煌"为主题，汇聚全球顶尖的机器人企业和创新技术。</p>
            <h4>主要亮点：</h4>
            <ul>
              <li>超过500家展商参展</li>
              <li>展示最新机器人技术和应用</li>
              <li>举办多场技术交流会和论坛</li>
              <li>提供商务对接服务</li>
            </ul>
            <p>我们诚挚邀请您参加这次盛会，共同探讨机器人产业的发展趋势和未来机遇。</p>
          `,
          image:
            "https://images.unsplash.com/photo-1581091226825-a6a2a5aee158?w=800&h=400&fit=crop",
          date: "2024-12-15",
          category: "展会动态",
        },
        {
          id: 2,
          title: "机器人产业发展趋势分析报告发布",
          content: `
            <h3>产业发展趋势分析</h3>
            <p>最新发布的机器人产业发展趋势分析报告显示，工业机器人市场将持续增长。</p>
            <p>服务机器人和协作机器人将成为未来发展的重点领域。报告指出，随着人工智能技术的不断进步，机器人将在更多领域发挥重要作用。</p>
            <h4>主要趋势：</h4>
            <ul>
              <li>工业机器人市场持续增长</li>
              <li>服务机器人应用场景扩大</li>
              <li>协作机器人技术日趋成熟</li>
              <li>人工智能与机器人深度融合</li>
            </ul>
          `,
          image:
            "https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=800&h=400&fit=crop",
          date: "2024-12-14",
          category: "行业报告",
        },
      ];

      return defaultNews.find((news) => news.id == newsId) || null;
    };

    // 返回上一页
    const goBack = () => {
      router.go(-1);
    };

    // 组件挂载时加载数据
    onMounted(() => {
      loadNewsDetail();
    });

    return {
      loading,
      newsData,
      goBack,
    };
  },
};
</script>

<style scoped>
.news-detail {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  height: 400px;
  color: white;
  /* background: linear-gradient(
    15deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  backdrop-filter: blur(10px); */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.loading-container {
  padding: 2rem 0;
}

.empty-container {
  padding: 4rem 0;
  text-align: center;
}

.news-title {
  font-size: 2.5rem;
  color: #333;
  margin-bottom: 1.5rem;
  line-height: 1.3;
  text-align: center;
}

.news-meta {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.news-date,
.news-category {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #666;
  font-size: 1rem;
}

.news-category {
  background: #667eea;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.9rem;
}

.news-image-section {
  margin: 2rem 0;
  text-align: center;
}

.news-image {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.news-body {
  margin: 2rem 0;
}

.news-full-content {
  color: #333;
  line-height: 1.8;
  font-size: 1.1rem;
}

/* 富文本内容样式 */
.news-full-content :deep(h1),
.news-full-content :deep(h2),
.news-full-content :deep(h3),
.news-full-content :deep(h4) {
  color: #333;
  margin: 1.5rem 0 1rem 0;
  font-weight: 600;
}

.news-full-content :deep(p) {
  margin-bottom: 1rem;
  text-align: justify;
}

.news-full-content :deep(ul),
.news-full-content :deep(ol) {
  margin: 1rem 0;
  padding-left: 2rem;
}

.news-full-content :deep(li) {
  margin-bottom: 0.5rem;
}

.news-full-content :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5rem auto;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.news-full-content :deep(blockquote) {
  border-left: 4px solid #667eea;
  padding-left: 1rem;
  margin: 1.5rem 0;
  color: #666;
  font-style: italic;
}

.news-actions {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid #eee;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 1rem;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .news-title {
    font-size: 1.8rem;
  }

  .news-meta {
    flex-direction: column;
    gap: 1rem;
    align-items: center;
  }

  .news-actions {
    flex-direction: column;
    align-items: center;
  }
}
</style>
